#!/usr/bin/python
#coding:utf-8
import linecache #用于读取某一行
import time,datetime
import os
import string
#from Tkinter import *
import tkFileDialog
#import win32ui
print os.name
filename = tkFileDialog.askopenfilename(initialdir = './')
date_str= os.path.split(filename)[1][0:8] #获得取文件日期
t_start=time.time() #获取当前时间戳，用于计算处理时间
dt_diff_std=datetime.timedelta(0,30)  #标准时间差
#print dt_diff_std
if(os.path.exists(date_str+'break.txt')==True):
    os.remove(date_str+'break.txt')
with open(date_str+'break.txt','a') as f_w:
                f_w.write('行号\t'+'中断时间\t'+'恢复时间\t'+'时间差\t\n')
with open(filename,'r') as f_tmp:
    #dt_pre = datetime.datetime.strptime(next(f_tmp).split()[0],"%H:%M:%S.%f")#读取第一行
    #dt_pre = datetime.datetime.strptime(linecache.getline(f_tmp.name,1).split()[0],"%H:%M:%S.%f")#读取第一行时刻作为比较初始值
    dt_pre = datetime.datetime.strptime('0:0:0.0',"%H:%M:%S.%f")#以零点作为比较初始值

    #print next(f_tmp)
    #content = f_tmp.readlines()
        
    for num,line1 in enumerate(f_tmp): #续行读取
        
        try:
            dt_cur=datetime.datetime.strptime(line1.split()[0].lstrip(chr(65)+'\0 '),"%H:%M:%S.%f")#去除前面的空白字符，读取当前时刻
            #dt_cur=datetime.datetime.strptime(filter(lambda x: x in string.printable, line1).split()[0],"%H:%M:%S.%f")#读取当前时刻
        except Exception as err:
            #print type(err)
            continue
        dt_diff=dt_cur- dt_pre  #算时间差 
        if (dt_diff>=dt_diff_std):#间隔是否过大                     
            with open(date_str+'break.txt','a') as f_w:
                f_w.write(str(num)+dt_pre.strftime("\t%H:%M:%S.%f\t")+dt_cur.strftime("%H:%M:%S.%f\t")+str(dt_diff)+'\n')
                #print dt_diff
        dt_pre=dt_cur #保存当前时刻用于下次比较
t_start=time.time()-t_start
print '运行时间'+str(t_start)
print 'end'

#line1= linecache.getline(f_tmp.name,1)
#line2= linecache.getline(f_tmp.name,2)
#s1=
#s2=line2.split()

#print len(s1)
'''
print f_tmp.name[0:8]+s1[0]
print f_tmp.name[0:8]+s2[0]
dt1=datetime.datetime.strptime(f_tmp.name[0:8]+s1[0],"%Y%m%d%H:%M:%S.%f")
dt2=datetime.datetime.strptime(f_tmp.name[0:8]+s2[0],"%Y%m%d%H:%M:%S.%f")
#t1=time.mktime(dt1.timetuple())
#t2=time.mktime(dt2.timetuple())
dt3=dt2-dt1
print dt_diff_std>dt3
'''




